<?php
//公共方法

 function pub($config){
 @mysql_connect($config['host'],$config['user'],$config['pass']);
if(mysql_errno()){
die(mysql_error());
}
mysql_select_db($config['db']);
mysql_set_charset($config['char']);
}

// 查询函数

 function sel($config,$table,$where=''){
   pub($config);
   $sql="select * from {$table} {$where}";
   // var_dump($sql);die;
   // var_dump($sql);die;
   $res=mysql_query($sql);
   // var_dump($res);die;

   $return=array();
   while ($row=mysql_fetch_assoc($res)) {
        $return[] = $row;
      }   
   return $return;
    mysql_free_result($res);
mysql_close();
 }

 // 删除函数

function del($config,$table,$where){
  pub($config);
  $sql="delete from {$table} {$where}";

  $res=mysql_query($sql);
  return $res;
mysql_free_result($res);
mysql_close();
}

function add($config,$table,$post){
  pub($config);
  $keys=implode('`,`',array_keys($post));
  $values=implode("','",array_values($post));

  $sql="insert into {$table}(`{$keys}`) values('{$values}')";
  // echo $sql;die;
  $res=mysql_query($sql);
  // var_dump($res);die ;
  return $res;
}

function up($config,$table,$post){
 
  pub($config);
  $str='';
  foreach ($post as $k => $v) {
    if ($k!='id')
    {
      $str.="`{$k}`='{$v}',";
    } 
  }
  $sql = "update {$table} set " . rtrim($str, ',') . " where id=" . $post['id'];
  var_dump($sql);die;
  $res=mysql_query($sql);
  return $res;
}
// $sql="update {$table} set {str} 'where id='.$post['id']";





function page ($config,$table,$sum=3,$leftright=1,$color='red'){

   
$sum=3;
$p=isset($_GET['p'])&&!empty($_GET['p'])?$_GET['p']:1;
$off=($p-1)*3;

pub($config);
$sql="select * from {$table} limit {$off},{$sum}";
// var_dump($sql);die;
$sqlcount="select count(*) as a from {$table}";
$res=mysql_query($sql);
$rescount=mysql_query($sqlcount);
$rowcount=mysql_fetch_assoc($rescount);
$rowcount=$rowcount['a'];
// var_dump($rowcount);die;
$count=ceil($rowcount/$sum);

$prev=$p-1;
if($prev<=0){
  $prev=1;
}
$next=$p+1;
if ($next>$count){
  $next=$count;
}

$leftright=1;
if ($leftright*2+1>=$count) {
$left=1;
$right=$count;
}else{
if ($p-$leftright>0) {
  $left=$p-$leftright;
  $right=$p+$leftright;

}else{$left=1;
$right=$leftright*2+1;
}
if(!isset($right)){
if ($p+$leftright>$count){
  $right=$count;
  $left=$count-$leftright*2;
}else{
$right=$p+$leftright;
}
}
}
$str='';
$str.= '<a href="'.$_SERVER['PHP_SELF'].'?p='.$prev.'">上一页</a>';

if ($left!=1) {
  $str.='...';
}
for ($i=$left; $i<=$right; $i++) { 
  if ($i==$p) {
    $color='red';
  }else{$color='';}
  $str.= '<a style="color:'.$color.'" href="'.$_SERVER['PHP_SELF'].'?p='.$i.'">'.$i.'</a>';
}

if ($right!=$count) {
  $str.='...';
}

$str.= '<a href="'.$_SERVER['PHP_SELF'].'?p='.$next.'">下一页</a>';
$str.= '<a href="'.$_SERVER['PHP_SELF'].'?p=1">首页</a>';
$str.= '<a href="'.$_SERVER['PHP_SELF'].'?p='.$count.'">尾页</a>';
$str.= '<form>';
$str.= '<input type="text" name=p>';
$str.= '<input type="submit">';
$str.= '</form>';
$str.= "总共{$count}页,当前{$p}/{$count}页";
// echo $str;
return $str;
}


function upload ($name, $dir = '/public/upload/', $max_size = 2097157, $allow_type = array('jpg', 'png', 'gif'))
{
    $file = $_FILES[$name];
    $ext  = trim(strchr($file['name'], '.'), '.');
    //判断错误号
    if ($file['error'] != 0) {
        echo '上传错误: ';
        switch ($_FILES['myfile']['error']) {
            case 1:die('上传文件大小超出了PHP配置中的约定值：upload_max_filesize');
            case 2:die('上传文件大小超出了表单中的约定值：MAX_FILE_SIZE');
            case 3:die('文件只被部分上载');
            case 4:die('没有上传任何文件');
            case 6:die('找不到临时文件夹');
            case 7:die('文件写入失败');
            default:die('末知错误');
        }
    }

    //判断上传的文件大小
    if ($file['size'] > $max_size) {
        die('上传文件太大了我受不了！');
    }

    //判断上传的文件格式
    if (!in_array($ext, $allow_type)) {
        die('你当我瞎啊，请传正确的格式！');
    }

    //随机文件名
    $filename = date('YmdHis') . mt_rand(100, 999) . '.' . $ext;
    // echo $filename;die;

    if (is_uploaded_file($file['tmp_name'])) {
        // if (!file_exists($dir)) {
        //     $res = mkdir($dir);
        // }
        if (move_uploaded_file($_FILES[$name]['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/'.trim($dir, '/') . '/' . $filename)) {
            return '/'.trim($dir, '/') . '/' . $filename;
        } else {
            echo '移动失败';
        }
    } else {
        die('非法上传');
    }
}
?>